﻿using _250210作业.DAL;
using _250210作业.Model;
using MVC_Project.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _250210作业.BLL
{
    public class UsersBLL : BaseBLL<Users>
    {
        public override void SetDal()
        {
            base.Dal = new UsersDAL();
        }
        public List<Users> GetUsers()
        {
            return base.QueryList();
        }

        public List<Users> GetUsersByPage(int page, int size, string name, out int count)
        {
            var users = Dal.Query();

            if (!string.IsNullOrWhiteSpace(name))
            {
                users = users.Where(u => u.loginName.Contains(name));
            }

            count = users.Count();

            var list = users.OrderBy(u => u.userID).Skip((page - 1) * size).Take(size).ToList();

            return list;
        }

        public bool Delete(int id)
        {
            var user = Dal.Query().Where(u => u.userID == id).FirstOrDefault();
            Dal.Delete(user);
            return Dal.SaveChanges();
        }

        /// <summary>
        /// 验证手机号码是否可用（不存在）
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public bool IsValidPhone(string phone)
        {
            var users = Dal.Query();

            var count = users.Where(u => u.phone == phone).Count();

            return count == 0;
        }

        //修改
        public bool EditIsValidPhone(string phone, int uid)
        {
            var users = Dal.Query();

            var count = users.Where(u => u.phone == phone && u.userID != uid).Count();

            return count == 0;
        }


    }

}
